// @Author EthanScriptOn
// @Desc
package component

import (
	"fmt"
	"golang.org/x/crypto/bcrypt"
)

func PasswordEncryption(originPassword string) (err error, hashedPassword string) {
	if originPassword == "" {
		return fmt.Errorf("originPassword is empty"), ""
	}
	hashedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(originPassword), bcrypt.DefaultCost)
	if err != nil {
		return fmt.Errorf("Failed to hash password: " + err.Error()), ""
	}
	hashedPassword = string(hashedPasswordBytes)
	return
}

func ConsistentJudgment(hashedPassword string, originPassword string) (err error, isConsistent bool) {
	if originPassword == "" {
		return fmt.Errorf("originPassword is empty"), false
	}
	if hashedPassword == "" {
		return fmt.Errorf("hashedPassword is empty"), false
	}
	err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(originPassword))
	return nil, err != nil
}
